<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">商品操作</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="商品名字">
        <el-input v-model="queryForm.goodsName" placeholder="商品名字"></el-input>
      </el-form-item>
      <el-form-item label="商品类型">
        <el-input v-model="queryForm.goodsType" placeholder="商品类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="goodsId" label="编号" style="0%"></el-table-column>
      <el-table-column prop="goodsName" label="商品名字" style="0%"></el-table-column>
      <el-table-column prop="goodsType" label="商品类型" style="0%"></el-table-column>
      <el-table-column prop="goodsPrice" label="商品价格" style="0%"></el-table-column>
      <el-table-column prop="goodsNums" label="商品数量" style="0%"></el-table-column>
      <el-table-column label="操作" style="0%">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope" >
            <el-button type="danger" size="mini" @click="sellGoods(scope.row,scope.$index)">出售</el-button>
            <el-button type="danger" size="mini" @click="soldOut(scope.row,scope.$index)">下架</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
    <el-form :model="goods" status-icon  ref="goods" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品名字" prop="saleGoodsName">
          <el-input
            v-model="goods.saleGoodsName"
            :disabled="true">
          </el-input>
      </el-form-item>
      <el-form-item label="商品类型" prop="saleGoodsType">
          <el-input
            v-model="goods.saleGoodsType"
            :disabled="true">
          </el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="saleGoodsPrice">
          <el-input
            v-model="goods.saleGoodsPrice"
            :disabled="true">
          </el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="saleGoodsNums">
          <el-input-number v-model="goods.saleGoodsNums" :min="1" :max="goods.goodsNums"></el-input-number>
      </el-form-item>
      <el-form-item label="买家电话" prop="saleGoodsBuyTele">
        <el-input v-model="goods.saleBuyTele"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="saleBuyEmail"
          :rules="[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ]"
      >
        <el-input style="width: 300px;" v-model="goods.saleBuyEmail"></el-input>
      </el-form-item>
        <el-button @click="submitForm()">出售</el-button>
    </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        return {
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          isShow:false,
          title:"出售",
          queryForm: {
            page: 1,
            row: 10,
            goodsName:"",
            goodsType:"",
          },
          goods:{
            goodsId:"",
            goodsNums:"",
            saleGoodsName:"",
            saleGoodsType:"",
            saleGoodsPrice:"",
            saleGoodsNums:"",
            saleBuyTele:"",
            saleBuyEmail:"",
          },
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/goods/goodsIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        sellGoods(x,index) {
          this.goods.goodsId = x.goodsId;
          this.goods.goodsNums = x.goodsNums;
          this.goods.saleGoodsName = x.goodsName;
          this.goods.saleGoodsType = x.goodsType;
          this.goods.saleGoodsPrice = x.goodsPrice;
          saleGoodsNums:"";
          saleBuyTele:"";
          saleBuyEmail:"";
          this.title = "出售商品";
          this.isShow = true;
        },
        soldOut(x,index) { //下架
          this.goods.goodsId = x.goodsId;
          var self = this
          var http = this.$http.post("api/goods/deleteGoods",this.$qs.stringify(this.goods));
          http.then(function(rs){
                alert(rs.data.msg)
                location.reload();
          })
        },
        submitForm(){
          var self = this
          var http = this.$http.post("/api/sale/joinSale",this.$qs.stringify(this.goods));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }else{
                alert(rs.data.msg)
              }
          })
        }
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
